<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>闭包的基本概念 | JavaScript 高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.2.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html" />
    
    
    <link rel="prev" href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="1.7" data-basepath=".." data-revision="Tue Mar 01 2016 06:46:18 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        JavaScript 高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="e9c9c586570ff813bf127694b025d155/index.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        面向对象基础复习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        原型的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        继承的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        Object 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Function 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        作用域链
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        闭包的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        getter 与 setter
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/index.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        面向对象高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        原型链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        函数的四中调用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        对象的创建模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        继承的方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        闭包案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        正则表达式对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        XMLHttpRequest 对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript 高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E9%97%AD%E5%8C%85%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5">&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x6982;&#x5FF5;</h1>
<p>&#x4E3B;&#x8981;&#x5185;&#x5BB9;:</p>
<ul>
<li>&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x6982;&#x5FF5;</li>
<li>&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x5F62;&#x5F0F;</li>
<li>&#x95ED;&#x5305;&#x4E0E;&#x5185;&#x5B58;</li>
</ul>
<p>&#x4E00;&#x822C;&#x4EBA;&#x8BA4;&#x4E3A;&#x95ED;&#x5305;&#x662F;&#x4E00;&#x4E2A;&#x795E;&#x79D8;&#x800C;&#x53C8;&#x6666;&#x6DA9;&#x7684;&#x5185;&#x5BB9;. &#x5B9E;&#x9645;&#x4E0A;&#x5E76;&#x975E;&#x5982;&#x6B64;. &#x63A5;&#x4E0B;&#x6765;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#x95ED;&#x5305;&#x7684;&#x6982;&#x5FF5;&#x548C;&#x95ED;&#x5305;&#x7684;&#x4E00;&#x822C;&#x7684;&#x5F62;&#x5F0F;, &#x540C;&#x65F6;&#x8BA8;&#x8BBA;&#x95ED;&#x5305;&#x7684;&#x6709;&#x70B9;&#x548C;&#x7F3A;&#x70B9;.</p>
<h2 id="%E9%97%AD%E5%8C%85%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5">&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x6982;&#x5FF5;</h2>
<p>&#x6240;&#x8C13;&#x7684;&#x95ED;&#x5305;, &#x7B80;&#x5355;&#x8BF4;&#x5C31;&#x662F;&#x5141;&#x8BB8;&#x4F4E;&#x7EA7;&#x5B50;&#x94FE;&#x8BBF;&#x95EE;&#x9AD8;&#x7EA7;&#x5B50;&#x94FE;&#x7684;&#x6570;&#x636E;&#x7684;&#x7ED3;&#x6784;.</p>
<p>&#x5728; &quot;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&quot; &#x4E00;&#x8282;&#x4E2D;&#x4ECB;&#x7ECD;&#x8FC7;, &#x9AD8;&#x7EA7;&#x5B50;&#x94FE;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x4F4E;&#x7EA7;&#x5B50;&#x94FE;&#x7684;&#x6570;&#x636E;, &#x4F46;&#x662F;&#x53CD;&#x8FC7;&#x6765;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> num1 = <span class="hljs-number">123</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num2 = <span class="hljs-number">456</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;1 &#x7EA7;&#x5B50;&#x94FE;&#x5141;&#x8BB8;&#x8BBF;&#x95EE; 0 &#x7EA7;&#x5B50;&#x94FE; &quot;</span> + num1);
}
foo();

<span class="hljs-keyword">try</span> {
    <span class="hljs-built_in">console</span>.log(num2);
} <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;0 &#x7EA7;&#x5B50;&#x94FE;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE; 1 &#x7EA7;&#x5B50;&#x94FE;&#x7684;&#x6570;&#x636E;&quot;</span>);
}
</code></pre>
<p>&#x4F46;&#x662F;&#x5982;&#x679C;&#x5141;&#x8BB8;&#x8BBF;&#x95EE;&#x8FD9;&#x4E2A;&#x6570;&#x636E;, &#x90A3;&#x5C31;&#x6784;&#x6210;&#x95ED;&#x5305;&#x7ED3;&#x6784;.</p>
<h3 id="%E9%97%AD%E5%8C%85%E7%9A%84%E9%80%9A%E4%BF%97%E5%90%AB%E4%B9%89">&#x95ED;&#x5305;&#x7684;&#x901A;&#x4FD7;&#x542B;&#x4E49;</h3>
<p>&#x95ED;&#x5305;, &#x4ECE;&#x5B57;&#x9762;&#x610F;&#x4E49;&#x4E0A;&#x6765;&#x89E3;&#x91CA;, &#x5C31;&#x662F;&#x5305;&#x88F9;&#x8D77;&#x6765;&#x7684;, &#x5C01;&#x95ED;&#x8D77;&#x6765;&#x7684;&#x7ED3;&#x6784;. &#x5728; JavaScript &#x4E2D;, &#x51FD;&#x6570;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5C01;&#x95ED;&#x7684;&#x7ED3;&#x6784;. &#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x6570;&#x636E;, &#x5728;&#x5916;&#x9762;&#x662F;&#x8BBF;&#x95EE;&#x4E0D;&#x5230;&#x7684;.</p>
<pre><code class="lang-javascript">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>,
        obj = {},
        arr = [],
        isTrue = <span class="hljs-literal">false</span>,
        str = <span class="hljs-string">&quot;abc&quot;</span>,
        foo = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{};
})();
</code></pre>
<p>&#x5728;&#x51FD;&#x6570;&#x5916;&#x9762;&#x662F;&#x7EDD;&#x5BF9;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;&#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x7684;&#x6570;&#x636E;&#x7684;. &#x56E0;&#x6B64;&#x5B83;&#x5C31;&#x6784;&#x6210;&#x4E00;&#x4E2A;&#x5C01;&#x95ED;&#x7684;&#x7ED3;&#x6784;. &#x4F46;&#x662F;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4F5C;&#x7528;, &#x56E0;&#x6B64;&#x5728; JavaScript &#x4E2D;&#x9700;&#x8981;&#x5C06;&#x5176;&#x5BF9;&#x5916;&#x5F00;&#x653E;&#x4E00;&#x4E9B;&#x63A5;&#x53E3;, &#x4EE5;&#x4FBF;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x6570;&#x636E;. &#x8FD9;&#x5C31;&#x662F;&#x8981;&#x6C42;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x636E;, &#x597D;&#x4E48;&#x51FD;&#x6570;&#x8981;&#x4E48;&#x5BF9;&#x8C61;.</p>
<h3 id="%E9%97%AD%E5%8C%85%E7%9A%84%E5%9F%BA%E6%9C%AC%E7%BB%93%E6%9E%84">&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x7ED3;&#x6784;</h3>
<p>&#x8981;&#x5728;&#x51FD;&#x6570;&#x5916;&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5185;&#x7684;&#x6570;&#x636E;, &#x6700;&#x7B80;&#x5355;&#x7684;&#x5C31;&#x662F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> num;
    }
}
</code></pre>
<p>&#x5F53;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;, &#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x5141;&#x8BB8;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x636E;. &#x90A3;&#x4E48;&#x5728;&#x51FD;&#x6570;&#x5916;&#x5C31;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5185;&#x7684;&#x6570;&#x636E;&#x4E86;. &#x4F46;&#x662F;&#x4EC5;&#x4EC5;&#x662F;&#x8BBF;&#x95EE;&#x8FD9;&#x4E2A;&#x6570;&#x636E;, &#x6709;&#x65F6;&#x9700;&#x8981;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x4FEE;&#x6539;. &#x56E0;&#x6B64;&#x65E2;&#x8981;&#x6C42;&#x83B7;&#x5F97;&#x6570;&#x636E;, &#x4E5F;&#x8981;&#x6C42;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x6570;&#x636E;. &#x56E0;&#x6B64;&#x5C31;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x51FD;&#x6570;, &#x90A3;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
    <span class="hljs-keyword">return</span> {
        get_Num: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">return</span> num;
        },
        set_Num: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value</span>) </span>{
            num = value;
        }
    }
}
<span class="hljs-keyword">var</span> o = foo();
<span class="hljs-built_in">console</span>.log(o.get_Num()); <span class="hljs-comment">// =&gt; 123</span>
o.set_Num(<span class="hljs-number">456</span>);
<span class="hljs-built_in">console</span>.log(o.get_Num()); <span class="hljs-comment">// =&gt; 456</span>
</code></pre>
<p>&#x5C0F;&#x7ED3;&#x4E00;&#x4E0B;&#x95ED;&#x5305;&#x7684;&#x8FD9;&#x4E2A;&#x7ED3;&#x6784;:</p>
<ul>
<li>&#x9996;&#x5148;&#x6709;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x6570;&#x636E;</li>
<li>&#x540C;&#x65F6;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x5141;&#x8BB8;&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x7684;&#x6570;&#x636E;</li>
<li>&#x8C03;&#x7528;&#x51FD;&#x6570;, &#x8FD4;&#x56DE;&#x5141;&#x8BB8;&#x8BBF;&#x95EE;&#x5185;&#x90E8;&#x6570;&#x636E;&#x7684;&#x51FD;&#x6570;</li>
<li>&#x5229;&#x7528;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;, &#x5141;&#x8BB8;&#x5728;&#x5916;&#x754C;&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5185;&#x5B9A;&#x4E49;&#x7684;&#x6570;&#x636E;</li>
</ul>
<h2 id="%E9%97%AD%E5%8C%85%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A1%88%E4%BE%8B">&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x6848;&#x4F8B;</h2>
<p>&#x6709;&#x4E86;&#x95ED;&#x5305;, &#x53EF;&#x4EE5;&#x505A;&#x5230;&#x4E8B;&#x60C5;&#x5C31;&#x53D8;&#x5F97;&#x975E;&#x5E38;&#x591A;&#x4E86;. &#x4E0B;&#x9762;&#x901A;&#x8FC7;&#x51E0;&#x4E2A;&#x6848;&#x4F8B;&#x6765;&#x8BF4;&#x660E;&#x95ED;&#x5305;&#x7684;&#x4F7F;&#x7528;</p>
<h3 id="%E4%BD%BF%E7%94%A8%E9%97%AD%E5%8C%85%E6%A8%A1%E6%8B%9F%E7%A7%81%E6%9C%89%E5%8F%98%E9%87%8F">&#x4F7F;&#x7528;&#x95ED;&#x5305;&#x6A21;&#x62DF;&#x79C1;&#x6709;&#x53D8;&#x91CF;</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// v3 &#x4F7F;&#x7528;&#x65B9;&#x6CD5;, v5 &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528; getter</span>
<span class="hljs-keyword">var</span> person = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">name, age, gender</span>) </span>{
    <span class="hljs-keyword">return</span> {
        get name() {
            <span class="hljs-keyword">return</span> name;
        },
        get age() {
            <span class="hljs-keyword">return</span> age;
        },
        get gender() {
            <span class="hljs-keyword">return</span> gender;
        }
    }
};

<span class="hljs-keyword">var</span> obj = person(<span class="hljs-string">&quot;jk&quot;</span>, <span class="hljs-number">19</span>, <span class="hljs-string">&quot;male&quot;</span>);

<span class="hljs-built_in">console</span>.log(obj.name);
<span class="hljs-built_in">console</span>.log(obj.age);
<span class="hljs-built_in">console</span>.log(obj.gender);
</code></pre>
<h3 id="%E4%BD%BF%E7%94%A8%E9%97%AD%E5%8C%85%E6%A8%A1%E6%8B%9F%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F">&#x4F7F;&#x7528;&#x95ED;&#x5305;&#x6A21;&#x62DF;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> sum = <span class="hljs-number">0</span>;
(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt;= <span class="hljs-number">100</span>; i++) {
    sum += i;
}
})();
<span class="hljs-built_in">console</span>.log(sum);
</code></pre>
<h3 id="%E4%BD%BF%E7%94%A8%E9%97%AD%E5%8C%85%E5%88%9B%E5%BB%BA%E5%94%AF%E4%B8%80%E6%A0%87%E8%AF%86%E7%AC%A6">&#x4F7F;&#x7528;&#x95ED;&#x5305;&#x521B;&#x5EFA;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#x7B26;</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> uniqueInteger = (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> count = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> count++;
    };
})();
</code></pre>
<h2 id="%E9%97%AD%E5%8C%85%E4%B8%8E%E5%86%85%E5%AD%98">&#x95ED;&#x5305;&#x4E0E;&#x5185;&#x5B58;</h2>
<p>&#x4F7F;&#x7528;&#x95ED;&#x5305;&#x53EF;&#x4EE5;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x7684;&#x7EC4;&#x7EC7;&#x4EE3;&#x7801;, &#x5C06;&#x4E0D;&#x9700;&#x8981;&#x5BF9;&#x5916;&#x516C;&#x5F00;&#x7684;&#x4EE3;&#x7801;&#x90FD;&#x5C01;&#x88C5;&#x8D77;&#x6765;, &#x4FDD;&#x62A4;&#x8D77;&#x6765;. &#x4F7F;&#x5F97;&#x4EE3;&#x7801;&#x66F4;&#x52A0;&#x7D27;&#x51D1;, &#x7EF4;&#x62A4;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;. &#x4F46;&#x662F;&#x4F7F;&#x7528;&#x95ED;&#x5305;, &#x4FDD;&#x5B58;&#x4E86;&#x79C1;&#x6709;&#x6570;&#x636E;, &#x5BB9;&#x6613;&#x9020;&#x6210;&#x5185;&#x5B58;&#x7684;&#x6CC4;&#x9732;.</p>
<p>&#x4E3A;&#x4E86;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;&#x7684;&#x89E3;&#x91CA;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;, &#x9996;&#x5148;&#x8BA8;&#x8BBA;&#x4E00;&#x4E0B; JavaScript &#x4E2D;&#x5185;&#x5B58;&#x7684;&#x7BA1;&#x7406;.</p>
<h3 id="%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86">&#x5185;&#x5B58;&#x7BA1;&#x7406;</h3>
<p>JavaScript &#x5177;&#x6709;&#x5783;&#x573E;&#x6536;&#x96C6;&#x7684;&#x529F;&#x80FD;. &#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5F53;&#x6267;&#x884C;&#x73AF;&#x5883;&#x4F1A;&#x8D1F;&#x8D23;&#x7BA1;&#x7406;&#x4EE3;&#x7801;&#x7684;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4F7F;&#x7528;&#x7684;&#x5185;&#x5B58;, &#x5305;&#x62EC;&#x5185;&#x5B58;&#x7684;&#x5206;&#x914D;&#x4E0E;&#x91CA;&#x653E;. &#x5728;&#x4F20;&#x7EDF;&#x7684;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x4E2D;, &#x4F8B;&#x5982; C, C++, &#x9700;&#x8981;&#x624B;&#x52A8;&#x7684;&#x7BA1;&#x7406;&#x5185;&#x5B58;, &#x7533;&#x8BF7;, &#x91CA;&#x653E;&#x90FD;&#x9700;&#x8981;&#x624B;&#x52A8;&#x7684;&#x5904;&#x7406;. &#x56E0;&#x6B64;&#x5BB9;&#x6613;&#x51FA;&#x73B0;&#x95EE;&#x9898;. &#x5728; JavaScript &#x4E2D;&#x4E0D;&#x9700;&#x8981;&#x8FD9;&#x4E48;&#x9EBB;&#x70E6;, &#x6240;&#x6709;&#x7684;&#x4E8B;&#x60C5;&#x90FD;&#x7531; JavaScript &#x5F15;&#x64CE;&#x53BB;&#x5B8C;&#x6210;.</p>
<p>&#x5E38;&#x89C1;&#x7684;&#x5185;&#x5B58;&#x7BA1;&#x7406;&#x65B9;&#x5F0F;&#x6709;&#x4E24;&#x79CD;: &#x6807;&#x8BB0;&#x5F0F;, &#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5F0F;.</p>
<p>&#x4F7F;&#x7528;&#x6807;&#x8BB0;&#x5F0F;&#x5185;&#x5B58;&#x7BA1;&#x7406;, &#x8FD0;&#x884C;&#x65F6;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x4F1A;&#x6807;&#x8BB0;&#x6240;&#x6709;&#x7684;&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;, &#x8FDB;&#x800C;&#x52A0;&#x4EE5;&#x56DE;&#x6536;. &#x5982;&#x4ECA;&#x5927;&#x90E8;&#x5206;&#x6D4F;&#x89C8;&#x5668;&#x6240;&#x91C7;&#x7528;&#x7684;&#x662F;&#x6807;&#x8BB0;&#x5F0F;&#x5185;&#x5B58;&#x7BA1;&#x7406;. &#x53EA;&#x662F;&#x8FDB;&#x884C;&#x5185;&#x5B58;&#x56DE;&#x6536;&#x7684;&#x65F6;&#x95F4;&#x4E0D;&#x540C;&#x800C;&#x5DF2;.</p>
<p>&#x4F7F;&#x7528;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5F0F;&#x5185;&#x5B58;&#x7BA1;&#x7406;, &#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x7ED9;&#x6BCF;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x52A0;&#x4E0A;&#x7F16;&#x53F7;, &#x51E1;&#x662F;&#x6709;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x8BE5;&#x5BF9;&#x8C61;, &#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5C31; +1
, &#x51E1;&#x662F;&#x51CF;&#x5C11;&#x4E00;&#x4E2A;&#x5F15;&#x7528;, &#x8BA1;&#x6570;&#x5C31; -1. &#x90A3;&#x4E48;&#x5F53;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x4E3A; 0 &#x7684;&#x65F6;&#x5019;, &#x56DE;&#x6536;&#x5185;&#x5B58;. &#x4F46;&#x662F;&#x4E00;&#x65E6;&#x51FA;&#x73B0;&#x5FAA;&#x73AF;&#x5F15;&#x7528;, &#x5C31;&#x5BB9;&#x6613;&#x9020;&#x6210;&#x5185;&#x5B58;&#x6CC4;&#x9732;.</p>
<!--

### 内存泄露

### 性能问题
-->
<h3 id="%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86">&#x5185;&#x5B58;&#x7BA1;&#x7406;</h3>
<p>&#x5728; JavaScript &#x4E2D;&#x4E0D;&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x8003;&#x8651;&#x5185;&#x5B58;&#x7EF4;&#x62A4;&#x7684;&#x95EE;&#x9898;, &#x6240;&#x6709;&#x7684;&#x5185;&#x5B58;&#x7EF4;&#x62A4;&#x90FD;&#x53EF;&#x4EE5;&#x7531; JavaScript &#x5F15;&#x64CE;&#x53BB;&#x5904;&#x7406;. &#x4F46;&#x662F;&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x63D0;&#x5347;&#x6027;&#x80FD;, &#x5EFA;&#x8BAE;&#x5728;&#x4E0D;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;, &#x5C06;&#x5176;&#x8BBE;&#x7F6E;&#x4E3A; <code>null</code>.</p>
<h2 id="%E5%B0%8F%E7%BB%93">&#x5C0F;&#x7ED3;</h2>
<ul>
<li>&#x95ED;&#x5305;&#x7684;&#x57FA;&#x672C;&#x6982;&#x5FF5;</li>
<li>&#x95ED;&#x5305;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</li>
<li>&#x95ED;&#x5305;&#x6848;&#x4F8B;</li>
<li>&#x5185;&#x5B58;&#x7B80;&#x4ECB;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html" class="navigation navigation-prev " aria-label="Previous page: 作用域链"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html" class="navigation navigation-next " aria-label="Next page: getter 与 setter"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
